//
//  QuartzView.m
//  TestDemo
//
//  Created by fengxijun on 3/25/16.
//  Copyright © 2016 goappbox. All rights reserved.
//

#import "QuartzView.h"

@implementation QuartzView

CGContextRef MyPDFContextCreate(const CGRect *inMediaBox, CFStringRef path) {
    CGContextRef myOutContext = NULL;
    CFURLRef url;
    CGDataConsumerRef dataConsumer;
    url = CFURLCreateWithFileSystemPath(NULL, path, kCFURLPOSIXPathStyle, false);
    if (url == NULL) {
        return NULL;
    }
    
    dataConsumer = CGDataConsumerCreateWithURL(url);
    
    if (dataConsumer == NULL) {
        CFRelease(url);
        return NULL;
    }
    
    myOutContext = CGPDFContextCreate(dataConsumer, inMediaBox, NULL);
    
    CGDataConsumerRelease(dataConsumer);
    CFRelease(url);
    
    return myOutContext;
}

//CGContextRef MyPDFContextCreate(const CGRect *inMediaBox, CFStringRef path) {
//    CFURLRef urlRef = CFURLCreateWithFileSystemPath(NULL, path, kCFURLPOSIXPathStyle, false);
//    if (urlRef == NULL) {
//        return NULL;
//    }
//    
//    CGContextRef contextRef = CGPDFContextCreateWithURL(urlRef, inMediaBox, NULL);
//    CFRelease(urlRef);
//    
//    return contextRef;
//}

- (void) drawRect:(CGRect)rect
{
    
//    CGRect mediaBox = self.bounds;
//    NSString *path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"pdf"];
//    
//    CGContextRef myPDFContext = MyPDFContextCreate(&mediaBox, (__bridge CFStringRef)path);
//    CFStringRef myKeys[1];
//    CFTypeRef myValues[1];
//    
//    myKeys[0] = kCGPDFContextMediaBox;
//    myValues[0] = (CFTypeRef) CFDataCreate(NULL,(const UInt8 *)&mediaBox, sizeof (CGRect));
//    
//    CFDictionaryRef pageDictionary = CFDictionaryCreate(NULL, (const void **) myKeys,
//                                                        (const void **) myValues, 1,
//                                                        &kCFTypeDictionaryKeyCallBacks,
//                                                        & kCFTypeDictionaryValueCallBacks);
//    
//    CGPDFContextBeginPage(myPDFContext, pageDictionary);
//    // ********** Your drawing code here **********
//    CGContextSetRGBFillColor (myPDFContext, 1, 0, 0, 1);
//    CGContextFillRect (myPDFContext, CGRectMake (0, 0, 200, 100 ));
//    CGContextSetRGBFillColor (myPDFContext, 0, 0, 1, .5);
//    CGContextFillRect (myPDFContext, CGRectMake (0, 0, 100, 200 ));
//    CGPDFContextEndPage(myPDFContext);
//    
//    CFRelease(pageDictionary);
//    CFRelease(myValues[0]);
//    CGContextRelease(myPDFContext);
    
//    NSLog(@"%s",__PRETTY_FUNCTION__);
//    CGContextRef contextRef = UIGraphicsGetCurrentContext();
//    [[UIColor redColor] set];
//    CGContextFillRect(contextRef, CGRectMake(20.f, 80.f, 100.f, 200.f));
//    [[UIColor blueColor] set];
////    CGContextFillRect(contextRef, CGRectMake(20.f, 120.f, 200.f, 100.f));
//    UIRectFillUsingBlendMode(CGRectMake(20.f, 180.f, 200.f, 100.f), kCGBlendModeColor);
}

@end
